﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.Management
{
    public partial class RoleManagementSelect : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                TextBox_RoleId.Text = Request.QueryString["roleid"].ToString();

                int myid = Convert.ToInt32(TextBox_RoleId.Text);
                //查询出当前作业中心ID对应的作业中心名称
                var myquery = (from a in db.TSCRB_JS_Role where a.角色ID == myid select a).Single();
                TextBox_RoleName.Text = myquery.角色名称;
                if (myquery.权限ID != null)
                {
                    string[] aa = myquery.权限ID.Split(',');
                    var q2 = from a in db.TSCRB_JS_Auths
                             select new
                             {
                                 a.权限ID,
                                 a.权限名称,
                                 check = aa.Contains(a.权限ID.ToString())
                             };
                    GridView_RightList.DataSource = q2;
                    GridView_RightList.DataBind();

                }
                else
                {
                    var q3 = from a in db.TSCRB_JS_Auths
                             select new
                             {
                                 a.权限ID,
                                 a.权限名称,
                                 check = false
                             };
                    GridView_RightList.DataSource = q3;
                    GridView_RightList.DataBind();

                
                
                
                }
                
                
               



            }
        }


        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string myRight = "";

            for (int i = 0; i < GridView_RightList.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_RightList.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("Checkbox_Right")).Checked)
                {
                    myRight = myRight + "," + myrows.Cells[0].Text;
                }


            }

            var myRightRow = (from a in db.TSCRB_JS_Role where a.角色ID == Convert.ToInt32(TextBox_RoleId.Text) select a).Single();

            if (myRight != "")
            {
                string xx = myRight.Substring(1, myRight.Length - 1);
                myRightRow.权限ID = xx;
            }
            else
            {
                myRightRow.权限ID = "";
            }


            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限成功');history.go(-2); </script>");
                //history.go(-2)用于返回到角色管理的主页面。
            }
            catch
            {

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限失败'); </script>");
            }
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/View/Management/RoleManagement.aspx");
        }
    }
}
